package com.lyf.shop.dao.Impl;

import com.lyf.shop.dao.CartDao;
import com.lyf.shop.entity.Cart;
import com.lyf.shop.entity.Product;
import com.lyf.shop.util.DataBaseUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanMapHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CartDaoImpl implements CartDao {
    private QueryRunner qr = new QueryRunner();
    @Override
    public Cart findCart(int uid, int pid) throws SQLException {
        String sql = "select c.cid,c.uid,c.pid,c.ccount,c.cnum,p.tid,p.pname,p.ptime,p.pimage,p.pprice,p.pstate,p.pinfo" +
                " from product p,cart c where p.pid=c.pid and c.uid=? and c.pid=?";
        Map<String,Object> query =qr.query(DataBaseUtil.getConnection(),sql,new MapHandler(),uid,pid);
        if (query==null){
            return null;
        }
        Cart cart = new Cart();
        Product p = new Product();
        try {
            BeanUtils.populate(cart,query);
            BeanUtils.populate(p,query);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        cart.setProduct(p);
        return cart;
    }

    @Override
    public int addCart(Cart cart) throws SQLException{
        return qr.update(DataBaseUtil.getConnection(),
                "insert into cart values (default,?,?,?,?)",
                cart.getUid(),cart.getProduct().getPid(),cart.getCcount(),cart.getCnum());
    }

    @Override
    public int updateCart(Cart cart) throws SQLException{
        return qr.update(DataBaseUtil.getConnection(),
                "update cart set ccount=?,cnum=? where cid=? and pid=?",
                cart.getCcount(),cart.getCnum(),cart.getCid(),cart.getPid());
    }

    @Override
    public List<Cart> findCart(int uid) throws SQLException {
        String sql = "select c.cid,c.uid,c.pid,c.ccount,c.cnum,p.tid,p.pname,p.ptime,p.pimage,p.pprice,p.pstate,p.pinfo" +
                " from product p,cart c where p.pid=c.pid and c.uid=?";
        List<Cart> cartList = new ArrayList<>();
        //new MapListHandler() 返回list集合其中包含map和集合，每个map就代表着每一行数据
        List<Map<String, Object>> query = qr.query(DataBaseUtil.getConnection(), sql, new MapListHandler(), uid);
        if(query == null){
            return null;
        }
        for (Map<String, Object> stringObjectMap : query) {
            //声明需要封装的实体类
            Cart cart = new Cart();
            Product product = new Product();
            //工具类封装
            try {
                BeanUtils.populate(cart,stringObjectMap);
                BeanUtils.populate(product,stringObjectMap);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            cart.setProduct(product);
            cartList.add(cart);
        }
        return cartList;
    }

    @Override
    public int delCart(int uid) throws SQLException {
        return qr.update(DataBaseUtil.getConnection(),"delete from cart where uid=?",uid);
    }

    @Override
    public int delCart(int uid, int pid) throws SQLException {
        return qr.update(DataBaseUtil.getConnection(),"delete from cart where uid=? and pid=?",uid,pid);
    }
}
